package com.thomas.generator.component;

import com.thomas.generator.component.impl.MysqlGeneratorImpl;
import com.thomas.generator.util.ClassUtil;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xutao
 * @Package com.thomas.generator.component
 * @Description: ${todo}
 * @date 2019/12/11 14:05
 */
public class ContextFactory {

    private static Map<String,IGenerator> generatorMap = new HashMap<>();

    static {
        MysqlGeneratorImpl mysqlGenerator = new MysqlGeneratorImpl();
        ArrayList<Class> list = ClassUtil.getAllClassByInterface(IGenerator.class);
        for(Class clz :list){
            try {
                if(!Modifier.isAbstract(clz.getModifiers())){ // 判断是否抽象类
                    IGenerator generator = (IGenerator) clz.newInstance();
                    generatorMap.put(generator.getDBType(),generator);
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }


    public void execute(String dbType){
        try {
            generatorMap.get(dbType).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
